﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
//
using Microsoft.SharePoint.Portal.WebControls;
//
//====================================================================================================================================================================
// User Rating Web Part for SharePoint 2010
// Provided as an example best practice - David M. Sterling - david_sterling@sterling-consulting.com
// Sterling International Consulting Group - www.sterling-consulting.com - Architects, Designers, Instructors
// Use at your own risk!
//
// This part is a way to provide on-page rating for both Pages and Blog Posts. This is an example of using a web part to overcome an issue in 
// the SharePoint User Interface (you can turn on ratings but they require navigation to the list to set).
//====================================================================================================================================================================
//
namespace UserRatingWebPart.UserRatingWP
{
    [ToolboxItemAttribute(false)]
    public class UserRatingWP : WebPart
    {

        #region Properties
        //
        // Title CSS Class to use for display:
        //
        string _displayDivCSSClass = "ms-WPHeader";
        [WebDisplayName("Display Div CSS Class (overall display) - default is ms-WPHeader")]
        [WebDescription("Tool tip: Display Div CSS Class (overall display) - default is ms-WPHeader")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public string DisplayDivCSSClass
        {
            get
            {
                return _displayDivCSSClass;
            }

            set
            {
                _displayDivCSSClass = value;
            }
        }
        //
        // Title to display on page (default Rating):
        //
        string _displayMainTitle = "Post Rating";
        [WebDisplayName("Title to display")]
        [WebDescription("Tool tip: Title to display")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public string DisplayMainTitle
        {
            get
            {
                return _displayMainTitle;
            }

            set
            {
                _displayMainTitle = value;
            }
        }
        //
        // Title CSS Class to use for display:
        //
        string _displayTitleCSSClass = "ms-CommentHeader";
        [WebDisplayName("Title CSS Class to use for display")]
        [WebDescription("Tool tip: Title CSS Class to use for display")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public string DisplayTitleCSSClass
        {
            get
            {
                return _displayTitleCSSClass;
            }

            set
            {
                _displayTitleCSSClass = value;
            }
        }
        //
        // Text to display on page (default Rating):
        //
        string _displayText = "Rate this post:";
        [WebDisplayName("Text to display (i.e. Rate this Page or Rate this Post) - uses default page text style")]
        [WebDescription("Tool tip: Text to display (i.e. Rate this Page or Rate this Post) - uses default page text style")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public string DisplayText
        {
            get
            {
                return _displayText;
            }

            set
            {
                _displayText = value;
            }
        }
        //
        // Don't display title:
        //
        bool _dontDisplayTitle = false;
        [WebDisplayName("Don't display title?")]
        [WebDescription("Tool tip: Don't display title?")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public bool DontDisplayTitle
        {
            get
            {
                return _dontDisplayTitle;
            }

            set
            {
                _dontDisplayTitle = value;
            }
        }
        //
        // Remove all borders:
        //
        bool _dontDisplayBorders = false;
        [WebDisplayName("Don't display top/bottom border?")]
        [WebDescription("Tool tip: Don't display top/bottom border?")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public bool DontDisplayBorders
        {
            get
            {
                return _dontDisplayBorders;
            }

            set
            {
                _dontDisplayBorders = value;
            }
        }
        //
        // Border Color:
        //
        string _displayBorderColor = "#ebebeb";
        [WebDisplayName("Color to use for 1px top/bottom border (#ebebeb is default)")]
        [WebDescription("Tool tip: Color to use for 1px top/bottom border (#ebebeb is default)")]
        [Personalizable(PersonalizationScope.Shared)]
        [Category("Web Part Settings")]
        [WebBrowsable(true)]
        public string DisplayBorderColor
        {
            get
            {
                return _displayBorderColor;
            }

            set
            {
                _displayBorderColor = value;
            }
        }
        #endregion
        //
        #region Controls
        //
        // Create the rating control (Note: Ratings must be enabled on the Pages or Post library for ratings to work):
        //
        AverageRatingFieldControl ARFC = new AverageRatingFieldControl();
        //
        #endregion
        //
        #region Methods
        //
        //============================================================================================================================================================
        /// <summary>
        /// CreateChildControls - Creates the controls
        /// </summary>
        //============================================================================================================================================================
        //
        protected override void CreateChildControls()
        {
            // Define the control settings:
            //
            ARFC.FieldName = "Rating (0-5)"; // Field name to read
            ARFC.ID = "PageRatingControl";   // ID of the control
            this.Controls.Add(ARFC);
        }
        //
        //============================================================================================================================================================
        /// <summary>
        /// OnPreRender - make sure the chrome is hidden
        /// </summary>
        //============================================================================================================================================================
        //
        protected override void OnPreRender(EventArgs e)
        {
            //this.ChromeType = PartChromeType.None;
        }
        //
        //============================================================================================================================================================
        /// <summary>
        /// Render - Render the ratings
        /// </summary>
        //============================================================================================================================================================
        //
        protected override void Render(HtmlTextWriter writer)
        {
            if (DontDisplayBorders)
            {
                writer.Write("<div class=\"" + DisplayDivCSSClass + "\" style=\"padding-bottom: 10px; padding-top:5px; vertical-align:middle; width:100%;\">");
            }
            else
            {
                writer.Write("<div class=\"" + DisplayDivCSSClass + "\" style=\"padding-bottom: 10px; padding-top:5px; vertical-align:middle; width:100%;border-bottom: " + DisplayBorderColor + " 1px solid; border-collapse: collapse; border-top: " + DisplayBorderColor + " 1px solid; border-right: transparent 1px solid; border-left: transparent 1px solid;\">");
            }
            if (!DontDisplayTitle)
            {
                writer.Write("<h3 class=\"" + DisplayTitleCSSClass + "\" style=\"font-size:1.6em; font-weight:normal;left: -2px !important; right: 2px !important; top: 0em; !important\">" + DisplayMainTitle + "</h3>");
            }
            writer.Write("<div style=\"float:left;display:inline-block; margin-top: 2px; margin-right:10px;\">" + DisplayText + "&nbsp;</div>");
            ARFC.RenderControl(writer);
            writer.Write("</div>");
        }
        //
        #endregion
        //
        //============================================================================================================================================================
        // End of class
        //============================================================================================================================================================
        //
    }
}
